<template>
    <div class="code-generate-page">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <el-button type="primary" @click="addOrUpdateHandle()">新增角色</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border v-loading="dataListLoading"
            style="width: 100%;">
            <el-table-column prop="id" header-align="center" align="center" width="70" label="主键" />
            <el-table-column prop="roleName" header-align="center" align="center" label="角色名称" />
            <el-table-column prop="workbenchCode" header-align="center" align="center" label="工作台编号" />
            <el-table-column prop="createDate" header-align="center" align="center" label="创建时间" />
            <el-table-column prop="updateDate" header-align="center" align="center" label="更新时间" />
            <el-table-column header-align="center" align="center" width="250" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="addOrUpdateHandle(scope.row)">修改</el-button>
                    <el-button type="primary" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
                    <el-button type="primary" size="mini" @click="grantPermission(scope.row.id)">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗-新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
        <!-- 弹窗-分配权限 -->
        <role-grant-permission v-if="grantPermissionVisible" ref="grantPermission" @refreshDataList="getDataList" />
    </div>
</template>

<script>
import AddOrUpdate from './role-add-or-update'
import RoleGrantPermission from './role-grant-permission'
export default {
    data() {
        return {
            dataForm: {
                key: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            dataListSelections: [],
            addOrUpdateVisible: false,
            grantPermissionVisible: false
        }
    },
    components: {
        AddOrUpdate,
        RoleGrantPermission
    },
    activated() {
        this.getDataList()
    },
    methods: {
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true
            this.$http({
                url: '/pms/admin/role/listRoleInfo',
                method: 'post',
                data: {
                    pageInfo: {
                        pageNum: this.pageIndex,
                        pageSize: this.pageSize
                    }
                }
            }).then((response) => {
                if (response && response.code == "0") {
                    this.dataList = response.data
                    this.totalPage = response.total
                } else {
                    this.dataList = []
                    this.totalPage = 0
                }
                this.dataListLoading = false
            })
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val
            this.pageIndex = 1
            this.getDataList()
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val
            this.getDataList()
        },
        // 新增 / 修改
        addOrUpdateHandle(data) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(data)
            })
        },
        // 删除
        deleteHandle(id) {
            this.$confirm('确定要删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: '/pms/admin/role/deleteRole',
                    method: 'post',
                    data: {
                        roleId: id
                    }
                }).then((response) => {
                    this.getDataList()
                    if (response && response.code == "0") {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1000
                        })
                    } else {
                        this.$message.error(response.message)
                    }
                })
            })
        },
        grantPermission(id) {
            this.grantPermissionVisible = true;
            this.$nextTick(() => {
                this.$refs.grantPermission.init(id);
            })
        }
    }
}
</script>
